﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.DAL.DocumentCenter
{
    public partial class TipoDeRadicado {
        public IEnumerable<ElementoDeConfiguracionDeEstructuraDeRadicado> ElementoDeConfiguracionDeEstructuraDeRadicadoOrdenados {
            get {
                return this.ElementoDeConfiguracionDeEstructuraDeRadicado.OrderBy(config => config.Orden);
            }
        }
    }

    public partial class Radicado : DocumentSet, ICustomMapping
    {

        #region fields

        TaxonomyFieldValue _document_Country;
        TaxonomyFieldValue _document_Category;
        TaxonomyFieldValue _document_Kind;
        TaxonomyFieldValue _status;
        DateTime _created;
        int? _document_Documental_Type_ID;
        SPFieldMultiColumnValue _childDocuments;
        string _tipoRadicado;
        Guid _listId;

        #endregion

        public Guid ListId
        {
            get { return _listId; }
            set { _listId = value; }
        }

        public string TipoRadicado
        {
            get { return _tipoRadicado; }
            set { _tipoRadicado = value; }
        }

        public string ParentExpedienteListName { get; set; }

        public int? Document_Documental_Type_ID
        {
            get { return _document_Documental_Type_ID; }
            set
            {
                if ((value != _document_Documental_Type_ID))
                    this.OnPropertyChanging("Document_Documental_Type_ID", _created);
                _document_Documental_Type_ID = value;
                this.OnPropertyChanged("Document_Documental_Type_ID");
            }
        }

        public IEnumerable<TraceabilityEntry> LogEntries { get; set; }

        public IEnumerable<DeliveryTraceabilityEntry> DeliveryLogs { get; set; } //This field only exists in outcome documents

        public DateTime Created
        {
            get { return _created; }
            set
            {
                if ((value != _created))
                    this.OnPropertyChanging("Created", _created);
                _created = value;
                this.OnPropertyChanged("Created");
            }

        }

        public TaxonomyFieldValue Categories
        {
            get { return _document_Category; }
            set
            {
                if ((value != _document_Category))
                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCUMENT_CATEGORY, _document_Category);
                _document_Category = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCUMENT_CATEGORY);
            }

        }

        public TaxonomyFieldValue Status
        {
            get { return _status; }
            set
            {
                if ((value != _status))
                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCUMENT_STATUS, _status);
                _status = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCUMENT_STATUS);
            }

        }

        public TaxonomyFieldValue Countries
        {
            get { return _document_Country; }
            set
            {
                if ((value != _document_Country))
                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCUMENT_COUNTRY, _document_Country);
                _document_Country = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCUMENT_COUNTRY);
            }
        }

        public TaxonomyFieldValue Tipo
        {
            get { return _document_Kind; }
            set
            {
                if ((value != _document_Kind))

                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCSET_KIND, _document_Kind);
                _document_Kind = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCSET_KIND);
            }
        }

        public String Titulo
        {
            get;
            set;
        }

        public List<FileEntity> AttachedFiles { get; set; }

        public List<ContactsDTO> AttachedCopys { get; set; }

        public string AttachedDocuments //Eliminar esta propiedad
        {
            get;
            set;
        }

        public SPFieldMultiColumnValue ChildDocuments
        {
            get { return _childDocuments; }
            set
            {
                if ((value != _childDocuments))

                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCSET_ATTACHED_DOCUMENTS, _document_Kind);
                _childDocuments = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCSET_ATTACHED_DOCUMENTS);
            }
        }


        [CustomMapping(Columns = new String[] { "Document_Department", "Document_Country", "Document_Category", "Document_Kind", "Created", "Document_Documental_Type_ID", "AttachedDocuments", "Document_Status" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.Countries = item[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] as TaxonomyFieldValue;
            this.Categories = item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] as TaxonomyFieldValue;
            this.Tipo = item[CustomSiteColumns.FIELD_DOCSET_KIND] as TaxonomyFieldValue;
            this.Titulo = item[CustomSiteColumns.FIELD_DOCSET_DESCRIPCION] as string;
            this.Document_Documental_Type_ID = item[CustomSiteColumns.FIELD_DOCUMENT_DCOUMENTAL_TYPE_ID] == null ? new Nullable<int>() : int.Parse(item[CustomSiteColumns.FIELD_DOCUMENT_DCOUMENTAL_TYPE_ID].ToString());
            this.ChildDocuments = item[CustomSiteColumns.FIELD_DOCSET_ATTACHED_DOCUMENTS] as SPFieldMultiColumnValue;
            this._tipoRadicado = this.Tipo == null ? string.Empty : this.Tipo.Label;
            this._created = (DateTime)item[SPBuiltInFieldId.Created];
            this.Status = item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] as TaxonomyFieldValue;
            this._listId = item.ParentList.ID;
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] = this.Countries;
            item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] = this.Categories;
            item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = this.Status;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {
        }

    }

    public partial class RadicadoInterno : Radicado
    {
        public SPFieldUserValueCollection SelectedDestinatario { get; set; }
    }

    public partial class ConfiguracionDepartamento : Item, ICustomMapping
    {

        #region fields

        TaxonomyFieldValue _document_Department;

        #endregion

        #region Properties

        public TaxonomyFieldValue Department
        {
            get { return _document_Department; }
            set
            {
                if ((value != _document_Department))

                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCSET_DEPARTMNET, _document_Department);
                _document_Department = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCSET_DEPARTMNET);
            }
        }

        #endregion

        [CustomMapping(Columns = new String[] { "Document_Department" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.Department = item[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET] as TaxonomyFieldValue;
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET] = this.Department;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {
            SPListItem originalItem = (SPListItem)originalListItem;
            SPListItem databaseItem = (SPListItem)databaseListItem;

            TaxonomyFieldValue originalCountriesValue = (TaxonomyFieldValue)originalItem[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET];
            TaxonomyFieldValue dbCountriesValue = (TaxonomyFieldValue)databaseItem[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET];

            if (mode == RefreshMode.OverwriteCurrentValues)
            {
                this.Department = dbCountriesValue;
            }
            else if (mode == RefreshMode.KeepCurrentValues)
            {
                databaseItem[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET] = this.Department;
            }
            else if (mode == RefreshMode.KeepChanges)
            {
                if (this.Department != originalCountriesValue)
                {
                    databaseItem[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET] = this.Department;
                }
                else if (this.Department == originalCountriesValue && this.Department != dbCountriesValue)
                {
                    this.Department = dbCountriesValue;
                }
            }
        }
    }

    public partial class Archivo : Document, ICustomMapping
    {

        #region fields

        SPFieldLookupValue _author;
        TaxonomyFieldValue _document_Category;

        #endregion

        #region Properties

        public SPFieldLookupValue Author
        {
            get { return _author; }
            set
            {
                if ((value != _author))
                    this.OnPropertyChanging("Author", value);
                _author = value;
                this.OnPropertyChanged("Author");
            }
        }

        public TaxonomyFieldValue Category
        {
            get { return _document_Category; }
            set { _document_Category = value; }
        }
        public SPFile File { get; private set; }

        public string DocumentUrl
        {
            get { return this.Path + "/" + this.Name; }
        }

        #endregion

        #region Implemented Properties

        [CustomMapping(Columns = new String[] { "Author", "Document_Category", "Created" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.Author = new SPFieldLookupValue(item[SPBuiltInFieldId.Author].ToString());
            this.Category = item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] as TaxonomyFieldValue;
            this.File = item.File;
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[SPBuiltInFieldId.Author] = this.Author;
            item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] = this.Category;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {

        }

        #endregion

        #region Public Methods


        #endregion

    }

    public partial class SolicitudVB : LinkToADocument, ICustomMapping
    {

        #region fields

        SPFieldLookupValue _author;

        #endregion

        #region Properties

        public SPFieldLookupValue Author
        {
            get { return _author; }
            set
            {
                if ((value != _author))
                    this.OnPropertyChanging("Author", value);
                _author = value;
                this.OnPropertyChanged("Author");
            }
        }

        #endregion

        #region Implemented Properties

        [CustomMapping(Columns = new String[] { "Author" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.Author = new SPFieldLookupValue(item[SPBuiltInFieldId.Author].ToString());
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[SPBuiltInFieldId.Author] = this.Author;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {

        }

        #endregion

    }

    public partial class Personas : Contactos
    {
        public string NombreCompleto
        {
            get
            {
                return this.Nombres + " " + this.PrimerApellido + " " + this.SegundoApellido;
            }
        }
    }

    public partial class Expediente : Folder, ICustomMapping
    {

        #region fields

        DateTime _created;

        #endregion

        #region Properties

        public DateTime Created
        {
            get { return _created; }
            set { _created = value; }
        }

        #endregion

        public string SubserieName { get; set; }

        public string SerieName { get; set; }

        [CustomMapping(Columns = new String[] { "Created" })]
        public void MapFrom(object listItem)
        {
            SPListItem currentItem = listItem as SPListItem;
            _created = (DateTime)currentItem[SPBuiltInFieldId.Created];
        }

        public void MapTo(object listItem)
        {

        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {
        }
    }

    public partial class RadicadoEntrante : Item, ICustomMapping
    {

        #region fields

        DateTime? _created;

        #endregion

        #region Properties

        public DateTime? Created
        {
            get { return _created; }
            set
            {
                if ((value != _created))
                    this.OnPropertyChanging("Created", _created);
                _created = value;
                this.OnPropertyChanged("Created");
            }
        }

        #endregion

        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.Created = item[SPBuiltInFieldId.Created] == null ? new Nullable<DateTime>() : (DateTime)item[SPBuiltInFieldId.Created];
        }

        public void MapTo(object listItem)
        {
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {

        }
    }

    public partial class EnvíoDeCorrespondencia : Item, ICustomMapping
    {

        #region [Private Fields]

        private TaxonomyFieldValue _City;

        #endregion

        #region [Public Properties]

        public TaxonomyFieldValue City
        {
            get { return _City; }
            set
            {
                if ((value != _City))
                    this.OnPropertyChanging("City", _City);
                _City = value;
                this.OnPropertyChanged("City");
            }
        }

        public string DescripcionModoEnvio 
        {
            get { return MedioEnvioCorreo.Title; } 
        }

        public string TipoEnvio 
        {
            get { return this.EnvioNacional.HasValue && EnvioNacional.Value ? "Nacional" : "Local"; }
        }

        public string NombreZona 
        {
            get { return this.ZonaEnvio.Title; } 
        }

        #endregion

        #region [ICustomMapping Members]

        [CustomMapping(Columns = new String[] { "Document_Country" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            this.City = item[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] as TaxonomyFieldValue;
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] = this.City;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem) { }

        #endregion
    }

    public partial class DocumentoImpreso : Item, ICustomMapping
    {
        #region fields

        DateTime _created;
        TaxonomyFieldValue _pais;

        #endregion

        #region Properties


        public TaxonomyFieldValue Pais
        {
            get { return _pais; }
            set
            {
                if ((value != _pais))
                    this.OnPropertyChanging(CustomSiteColumns.FIELD_DOCUMENT_COUNTRY, _pais);
                _pais = value;
                this.OnPropertyChanged(CustomSiteColumns.FIELD_DOCUMENT_COUNTRY);
            }

        }


        public DateTime Created
        {
            get { return _created; }
            set { _created = value; }
        }

        public bool IsCopy { get; set; }

        public EnvíoDeCorrespondencia DeliveryInfo { get; set; }

        public string City 
        {
            get
            {
                return Pais == null ? string.Empty : Pais.Label;
            }
        }

        #endregion

        [CustomMapping(Columns = new String[] { "Created", "Document_Country" })]
        public void MapFrom(object listItem)
        {
            SPListItem currentItem = listItem as SPListItem;
            _created = (DateTime)currentItem[SPBuiltInFieldId.Created];
            Pais = currentItem[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] == null ? null : ((TaxonomyFieldValue)currentItem[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY]);
        }

        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] = this.Pais;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem) { }
    }

}
